<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	public function index(){
		
	}

	function logout(){
		$this->session->sess_destroy();
		return true;
	}

	function logout_success(){
		$data['main_content'] = 'logout-success.php';
		$this->load->view('includes/template.php',$data);
	}

	function my_account(){
		$this->init();
	}

	function edit_my_account(){
		$this->load->library('form_validation');
		
		

		$this->form_validation->set_rules('current_password', 'Current Password', 'callback_currentPassword_check');
		$this->form_validation->set_rules('new_password', 'New Password', 'trim|required|matches[confirm_password]');
		$this->form_validation->set_rules('confirm_password', 'Confim Password', 'trim|required|matches[new_password]');
		$this->form_validation->set_error_delimiters('<span class="validation">', '</span>');

		$this->form_validation->set_message('matches', 'Your Passwords Do Not Match');
		if ($this->form_validation->run() == FALSE)
		{
			$this->init();
		}else{
			$this->load->model('users_model');
			$this->users_model->update_password(array('password' => md5($this->input->post('confirm_password')), 'id' => $this->get_user_id()));
			$this->init(true);
	}
	}

	public function currentPassword_check($pass){
		$this->load->library('form_validation');
		
		$user_obj = array('password' => md5($pass), 'id' => $this->get_user_id());
		$this->load->model('users_model');
		$checked = $this->users_model->validate_current_password($user_obj);
		if(!$checked){
			$this->form_validation->set_message('currentPassword_check', "Your password doesn't match existing password");
			return false;
		} else{
			return true;
		}
	}






	function init($valid = false){
		if ($valid) {
			$data['valid'] = TRUE;
		}else{
			$data['valid'] = FALSE;
		}
		$this->load->model('users_model');
		$data['username'] = $this->get_username();
		$data['main_content'] = 'my_account';
		$this->load->view('includes/template.php',$data);
	}

	function get_username(){
		$this->load->model('users_model');
		$session = $this->session->all_userdata();
		return $session['username'];
	}

	function get_user_id(){
		$this->load->model('users_model');
		$session = $this->session->all_userdata();
		return $session['id'];
	}

}

/* End of file user.php */
/* Location: ./application/controllers/user.php */